package com.thjk.bdf.common.utils;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public final class UidGenerator {
	/**
	 * 线程安全计数器
	 */
	private static AtomicInteger counter = new AtomicInteger(1);

	/**
	 * 根据ip获取流水号
	 * 
	 * @param ip
	 * @return
	 */
	public static String generateId(String ip) {
		String id = String.valueOf(System.currentTimeMillis());

		counter.compareAndSet(1000, 1);

		String[] sub2 = ip.split("\\.");
		String sub3 = String.format("%04d", counter.incrementAndGet());
		id = id + String.format("%03d", Integer.valueOf(sub2[2])) + String.format("%03d", Integer.valueOf(sub2[3]))
				+ sub3;

		return id;
	}
	
	/**
	 * 生成短信验证码
	 * 
	 * @return
	 */
	public static String getVerSmsCode() {
		String randNum = new Random().nextInt(8999) +  1000 +"";
		if (randNum.length() != 4) {
			return getVerSmsCode();
		}
		return randNum;
	}

}
